Imports KanBan.Pgd.Common
Imports NHibernate.Criterion
Imports KanBan.Pgd.Infrastructure.Exceptions
Imports NHibernate
Imports System.Data.SqlClient

Namespace EventLog

    Public Class EventLogger

        Public Shared Sub Log(ByVal level As EventLevel, ByVal ex As Exception)

            Dim log As New Log(DateTime.Now, ex.Message, level, ex.Source)

            SaveLogEntry(log)

        End Sub

        Public Shared Sub Log(ByVal message As String)

            Dim log As New Log(DateTime.Now, message, EventLevel.Info, String.Empty)

            SaveLogEntry(log)

        End Sub

        Private Shared Sub SaveLogEntry(ByVal log As Log)

            Try

                Dim conn As New SqlConnection(PgdSettings.ConnectionString)

                Dim cmd As New SqlCommand("RegisterLogEntry", conn)
                cmd.CommandType = CommandType.StoredProcedure

                cmd.Parameters.AddWithValue("@eventDate", log.EventDate)
                cmd.Parameters.AddWithValue("@message", log.Message)
                cmd.Parameters.AddWithValue("@level", log.Level)
                cmd.Parameters.AddWithValue("@source", log.Source)
                cmd.Parameters.AddWithValue("@thread", log.Thread)


                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()

            Catch ex As Exception

            End Try

        End Sub

        Public Shared Function GetAll() As IList(Of Log)

            Try

                Dim conn As New SqlConnection(PgdSettings.ConnectionString)
                Dim cmd As New SqlCommand("GetAllLogEntries", conn)
                cmd.CommandType = CommandType.StoredProcedure

                Dim da As New SqlDataAdapter()
                da.SelectCommand = cmd

                Dim logEntries As New DataSet()

                conn.Open()
                da.Fill(logEntries)
                conn.Close()

                Dim results As New List(Of Log)

                For Each r As DataRow In logEntries.Tables(0).Rows

                    results.Add(New Log(CInt(r.Item("logId")), _
                                        DateTime.Parse(r.Item("EventDate").ToString()), _
                                        r.Item("Message").ToString(), _
                                        CType(CInt(r.Item("Level")), EventLevel), _
                                        r.Item("Source").ToString()))

                Next

                Return results

            Catch ex As Exception

                Return New List(Of Log)

            End Try

        End Function

        Public Shared Function GetById(ByVal logId As Integer) As Log


            Try

                Dim conn As New SqlConnection(PgdSettings.ConnectionString)
                Dim cmd As New SqlCommand("GetLogEntry", conn)
                cmd.CommandType = CommandType.StoredProcedure

                cmd.Parameters.AddWithValue("@logId", logId)

                Dim da As New SqlDataAdapter()
                da.SelectCommand = cmd

                Dim logEntries As New DataSet()

                conn.Open()
                da.Fill(logEntries)
                conn.Close()

                Dim results As New List(Of Log)

                For Each r As DataRow In logEntries.Tables(0).Rows

                    results.Add(New Log(CInt(r.Item("logId")), _
                                        DateTime.Parse(r.Item("EventDate").ToString()), _
                                        r.Item("Message").ToString(), _
                                        CType(CInt(r.Item("Level")), EventLevel), _
                                        r.Item("Source").ToString()))

                Next

                Return results(0)

            Catch ex As Exception

                Return Nothing

            End Try

        End Function


    End Class

End Namespace
